class_name MusicManager
extends Node


enum Music {
	INTRO, MENU, STAGE01, STAGE02
}


const PREFAB_MUSIC_MAP: Dictionary[Music, AudioStreamMP3] = {
	Music.INTRO: preload("res://assets/music/intro.mp3"),
	Music.MENU: preload("res://assets/music/menu.mp3"),
	Music.STAGE01: preload("res://assets/music/stage-01.mp3"),
	Music.STAGE02: preload("res://assets/music/stage-02.mp3"),
}


@onready var musicStreamPlayer: AudioStreamPlayer = $MusicStreamPlayer


var autoplayed_stream: AudioStreamMP3 = null

func _ready():
	if autoplayed_stream:
		play_autoplayed_stream()


func play(music: Music):
	if musicStreamPlayer.is_node_ready():
		autoplayed_stream = PREFAB_MUSIC_MAP[music]
		play_autoplayed_stream()
	else:
		autoplayed_stream = PREFAB_MUSIC_MAP[music]


func play_autoplayed_stream():
	autoplayed_stream.loop = true
	musicStreamPlayer.stream = autoplayed_stream
	musicStreamPlayer.play()
